package com.zofer.study;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;

public class ThreadPoolExecutorTest {

    static ExecutorService poolService = new ThreadPoolExecutor(
            3,5,
            2, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(2),
            new ThreadFactoryBuilder().setNameFormat("zofer-pool").build(),
            new RejectedExecutionHandler(){
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    System.out.println("正在拒绝任务");
                }
            });


    static class Task implements Runnable{
        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName()+"正在执行...");
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"执行完了");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        for (int i = 0; i < 10 ; i++) {
            Task t1 = new Task();
            poolService.execute(t1);
        }
        System.out.println("主线程:"+Thread.currentThread().getName());
        poolService.shutdown();
    }
}
